<?php
class Jazar_Autolinks_Model_Parser {

    private static $instance = null;

    public static function getInstance () {
        if ( ! self::$instance )
            self::$instance = new self();

        return self::$instance;
    }

    public function fetch ( $html ) {
        $replacements = array();
		
		//print_r(Mage::getModel('autolinks/autolinks')->getCollection()->getSelect()->order('priority desc')->load());exit;
		$autolinksCollection = Mage::getModel('autolinks/autolinks')->getCollection();
		$autolinksCollection->getSelect()->order('priority desc');
        foreach ( $autolinksCollection as $row ) {
            if ( ! $row->getStatus() == Jazar_Autolinks_Model_Status::STATUS_ENABLED )
                continue;
                
            $replacements['#\b(\Q' . $row->getKeyword() . '\E)\b(?![^<>]*(?:>|</a>))#i'] = '<a href="' . $row->getHref() . '" class="seolink">\1</a>';
        }

        return preg_replace(array_keys($replacements), $replacements, $html);
    }
}
